package com.JavaWk2_8;

import java.util.Scanner;

public class franktions {

    private double f_Son, f_Mom;

    public franktions(){}

    public franktions(double s, double m){
        this.f_Son = s;
        this.f_Mom = m;
    }

    public String to_String(){
        return this.f_Son + "/" + this.f_Mom;
    }

    public double getF_Son() {
        return f_Son;
    }

    public void setF_Son(double f_Son) {
        this.f_Son = f_Son;
    }

    public double getF_Mom() {
        return f_Mom;
    }

    public void setF_Mom(double f_Mom) {
        this.f_Mom = f_Mom;
    }

    //求最大公约数
    public static double gcd(double a, double b){
        return b == 0 ? a : gcd(b, a % b);
    }

    //求最小公倍数
    public static double lcm(double a, double b){
        return a / gcd(a, b) * b;
    }

    public static franktions Reduction(franktions f){
        franktions t = new franktions();
        t = f;
        double mm = gcd(f.getF_Mom(), f.getF_Son());
//        System.out.println(mm);
        t.setF_Mom(t.getF_Mom() / mm);
//        System.out.println(t.getF_Mom() / mm);
//        System.out.println(t.getF_Son());
        t.setF_Son(t.getF_Son() / mm);
//        System.out.println(t.getF_Mom());
        return t;
    }

    public static franktions plus(franktions f, franktions f2){
        franktions t = new franktions();
        double mm = lcm(f.getF_Mom(), f2.getF_Mom());
        f.setF_Mom(mm);
        f2.setF_Mom(mm);
        f.setF_Son(f.getF_Son() * ((mm) / f.getF_Mom()));
        f2.setF_Son(f2.getF_Son() * ((mm) / f2.getF_Mom()));
        t.setF_Son(f.getF_Son() + f2.getF_Son());
        t.setF_Mom(mm);
        t = Reduction(t);
        return t;
    }

    public static franktions Subtraction(franktions f1, franktions f2){
        franktions t = new franktions();
        double mm = lcm(f1.getF_Mom(), f2.getF_Mom());
        f1.setF_Mom(mm);
        f2.setF_Mom(mm);
        f1.setF_Son(f1.getF_Son() * ((mm) / f1.getF_Mom()));
        f2.setF_Son(f2.getF_Son() * ((mm) / f2.getF_Mom()));
        t.setF_Son(f1.getF_Son() - f2.getF_Son());
        t.setF_Mom(mm);
        t = Reduction(t);
        return t;
    }

    public static franktions multiplication(franktions f1, franktions f2){
        franktions t = new franktions();
        t.setF_Mom(f1.getF_Mom() * f2.getF_Mom());
        t.setF_Son(f1.getF_Son() * f2.getF_Son());
        t = Reduction(t);
        return t;
    }

    public static franktions division(franktions f1, franktions f2){
        franktions t = new franktions();
         double tmp = f2.getF_Son();
         f2.setF_Son(f2.getF_Mom());
         f2.setF_Mom(tmp);
         t = multiplication(f1, f2);
        return t;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        franktions f1, f2, f3;
        f1 = new franktions(1, 3);
        f2 = new franktions(1, 4);
        System.out.print(f1.to_String() + " + " + f2.to_String() + " = ");
        System.out.println(plus(f1,f2).to_String());
        System.out.print(f1.to_String() + " - " + f2.to_String() + " = ");
        System.out.println(Subtraction(f1,f2).to_String());
        System.out.print(f1.to_String() + " * " + f2.to_String() + " = ");
        System.out.println(multiplication(f1,f2).to_String());
        System.out.print(f1.to_String() + " / " + f2.to_String() + " = ");
        System.out.println(division(f1,f2).to_String());
//        f3 = new franktions(2, 12);
//        f3 = Reduction(f3);
//        System.out.println(f3.to_String());
    }
}
